package com.yc.vote.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yc.vote.entity.Record;
import com.yc.vote.mapper.RecordMapper;
import com.yc.vote.service.IRecordService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 投票系统-投票记录表-6 服务实现类
 * </p>
 *
 * @author baomidou
 * @since 2024-04-16
 */
@Service
public class RecordServiceImpl extends ServiceImpl<RecordMapper, Record> implements IRecordService {
    @Transactional
    @Override
    public void commit(List<Record> records)throws BizException{
        if (records.isEmpty()){
            throw new BizException("请选择选项");
        }

        //判断是否重复投票
        LambdaQueryWrapper<Record> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Record::getVid,records.get(0).getVid());
        lqw.eq(Record::getUsid,records.get(0).getUsid());
        if (this.count(lqw)>0){
            throw new BizException("您已经给主题投过票了");
        }
        for (Record record: records){
            this.save(record);
        }
    }

}
